import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class AppService {
  public URL: string = "http://localhost/REST_API/";
  redirectURL: any;

  public accesstoken: any;

  constructor(private httpClient: HttpClient) { }

  public loginByAuth(username: any, password: any) {
    console.log(username);
    console.log(password);
    return this.httpClient.post<any>(this.URL + '/login.php', { username, password })
      .pipe(map(Users => {
        console.log(Users.username);
        this.setToken(Users.username);
        // this.getLoggedInName.emit(true);
        return Users;

      }))
  }

  public registerByAuth(username: any, password: any, phone: any) {
    return this.httpClient.post<any>(this.URL + '/register.php',
      {
        username, password, phone
      }).pipe(map(Users => {
        return Users;
      }));
  }

  public qualityDowload(accesstoken: any) {
    return this.httpClient.post<any>(this.URL + '/quality_dowload.php',
      {
        accesstoken
      }).pipe(map(Users => {
        return Users;
      }));
  }

  public resetPassword(username: any, phone: any, newpassword: any) {
    return this.httpClient.post<any>(this.URL + '/reset_password.php',
      {
        username, phone, newpassword
      }).pipe(map(Users => {
        return Users;
      }));
  }

  public addPlugin(plugin_name: any, plugin_detail: any, price: any, selectedFile : any, selectedFolder : any) {
    return this.httpClient.post<any>(this.URL + '/add_plugin.php',
      {
        plugin_name, plugin_detail, price, selectedFile, selectedFolder
      }).pipe(map(Users => {
        return Users;
      }));
  }


  public updatePluginByID(plugin_id : any, plugin_name: any, plugin_detail: any, price: any, selectedFile : any, selectedFolder : any) {
    return this.httpClient.post<any>(this.URL + '/update_plugin.php',
      {
        plugin_id, plugin_name, plugin_detail, price, selectedFile, selectedFolder
      }).pipe(map(Users => {
        return Users;
      }));

  }
  public addAdmin(username: any, password: any, phone: any, privileges : any) {
    return this.httpClient.post<any>(this.URL + '/add_admin.php',
      {
        username, password, phone, privileges
      }).pipe(map(Users => {
        return Users;
      }));

  }
  public updateUserByID(username: any, password: any, phone: any, privileges : any, user_id: any) {
    return this.httpClient.post<any>(this.URL + '/update_user.php',
      {
        username, password, phone, privileges, user_id
      }).pipe(map(Users => {
        return Users;
      }));

  }
  
  public loadUser(){
   return this.httpClient.get(this.URL + '/get_user.php').pipe(map(data => data));
  }
  public loadUserByID(user_id : any){
   return this.httpClient.post(this.URL + '/get_user_by_id.php', {user_id}).pipe(map(data => data));
  }
  public deleteUserByID(user_id : any){
   return this.httpClient.post<any>(this.URL + '/delete_user.php', {user_id}).pipe(map(data => data));
  }


  public loadPlugin(){
   return this.httpClient.get(this.URL + '/get_plugin.php').pipe(map(data => data));
  }
  public loadPluginByID(plugin_id : any){
   return this.httpClient.post<any>(this.URL + '/get_plugin_by_id.php', {plugin_id}).pipe(map(data => data));
  }
  public deletePluginByID(plugin_id : any){
   return this.httpClient.post<any>(this.URL + '/delete_plugin.php', {plugin_id}).pipe(map(data => data));
  }

  setToken(token: string) {
    localStorage.setItem('token', token);
  }
  destroyToken() {
    localStorage.removeItem('token');
  }

}

